home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / PowerD / powerd / examples / Bezier2.d < prev    next >
Text File  |  2002-10-28  |  3KB  |  117 lines

  1. // Bezier.d - example of how to generate bezier curves in D
  2. // requires workbench height of atleast ~340 pixels and fpu
  3.  
  4. MODULE    'intuition/intuition',
  5.             'utility/tagitem'
  6.  
  7. PROC main()
  8.     DEF    w:PTR TO Window,class,change=FALSE,code,x,y
  9.     DEF    id=-1,drag:PTR TO xy,count=5
  10.     IF w:=OpenWindowTags(NIL,
  11.             WA_InnerWidth,320,
  12.             WA_InnerHeight,320,
  13.             WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_MOUSEMOVE,
  14.             WA_Flags,WFLG_DRAGBAR|WFLG_GIMMEZEROZERO|WFLG_RMBTRAP|WFLG_ACTIVATE|WFLG_CLOSEGADGET|WFLG_DEPTHGADGET|WFLG_REPORTMOUSE,
  15.             WA_Title,'Bezier''s curve',
  16.             TAG_END)
  17.  
  18.         drag:=[            // draggable points
  19.             -100,-100,
  20.             +100,-50,
  21.             -100,0,
  22.             +100,+50,
  23.             -100,+100
  24.             ]:xy
  25.  
  26.         draw    // watch the definition below
  27.  
  28.         EasyRequestArgs(0,[SIZEOF_EasyStruct,0,0,'Drag those points and enjoy :)','Yea']:EasyStruct,0,NIL)
  29.  
  30.         WHILE (class,code:=WaitIMessage(w))<>IDCMP_CLOSEWINDOW
  31.             SELECT class
  32.             CASE IDCMP_MOUSEBUTTONS
  33.                 SELECT code
  34.                 CASE SELECTDOWN
  35.                     x:=w.GZZMouseX-160
  36.                     y:=w.GZZMouseY-160
  37.                     IF (id:=Drag(x,y,drag,count))>=0 THEN change:=TRUE
  38.                 DEFAULT
  39.                     id:=-1
  40.                 ENDSELECT
  41.             CASE IDCMP_MOUSEMOVE
  42.                 IF id>=0
  43.                     x:=w.GZZMouseX-160
  44.                     y:=w.GZZMouseY-160
  45.                     drag[id].x:=x
  46.                     drag[id].y:=y
  47.                     change:=TRUE
  48.                 ENDIF
  49.             ENDSELECT
  50.         NEXTIF change=FALSE
  51.             draw    // watch the definition below
  52.             change:=FALSE
  53.         ENDWHILE
  54.  
  55.         CloseWindow(w)
  56.     ENDIF
  57.  
  58.     SUB draw        // this is new feature from v0.16 of PowerD :)
  59.         SetRast(w.RPort,0)
  60.         DrawCV(w.RPort,drag,count)
  61.         Bezier(w.RPort,drag,count,100)
  62.     ENDSUB
  63.  
  64. ENDPROC
  65.  
  66. OBJECT xy
  67.     x/y:L
  68.  
  69. // Bezier() - creates a curve from a list of 4 points
  70.  
  71. PROC Bezier(rp,p:PTR TO xy,count,steps)
  72.     DEFF    delta,t,x,y,b
  73.     DEF    i,n
  74.     delta:=1.0/steps
  75.     SetAPen(rp,2)
  76.     Move(rp,p.x+160,p.y+160)
  77.     FOR i:=1 TO steps
  78.         t:=delta*i
  79.         x:=y:=0
  80.         FOR n:=0 TO count-1
  81.             b:=Ber(count-1,n,t)
  82.             x+=p[n].x*b
  83.             y+=p[n].y*b
  84.         ENDFOR
  85.         Draw(rp,x+160,y+160)
  86.     ENDFOR
  87. ENDPROC
  88.  
  89. PROC NabI(n,i)(F) IS Fac(n)/(Fac(i)*Fac(n-i))
  90. PROC Fac(i)(L)
  91.     DEFL    r=1
  92.     IF i THEN FOR i DTO 1 DO r*=i
  93. ENDPROC r
  94. PROC Ber(n,i,t:F)(F) IS NabI(n,i)*Pow(t,i)*Pow(1-t,n-i)
  95.  
  96. PROC DrawCV(rp,p:PTR TO xy,count)
  97.     DEF    i
  98.     SetAPen(rp,3)
  99.     RectFill(rp,p.x-2+160,p.y-2+160,p.x+2+160,p.y+2+160)
  100.     FOR i:=0 TO count-2
  101.         SetAPen(rp,1)
  102.         Move(rp,p[i].x+160,p[i].y+160)
  103.         Draw(rp,p[i+1].x+160,p[i+1].y+160)
  104.         SetAPen(rp,3)
  105.         RectFill(rp,p[i+1].x-2+160,p[i+1].y-2+160,p[i+1].x+2+160,p[i+1].y+2+160)
  106.     ENDFOR
  107. ENDPROC
  108.  
  109. PROC Drag(x,y,drag:PTR TO xy,count)(L)
  110.     DEF    id
  111.     FOR id:=0 TO count-1
  112.         IF x>=drag[id].x-2 AND x<=drag[id].x+2 AND y>=drag[id].y-2 AND y<=drag[id].y+2 THEN RETURN id
  113.     ENDFOR
  114. ENDPROC -1
  115.  
  116. // MarK 29/9/2000
  117.